package com.hualude.roadsign.service.impl;

import com.hualude.roadsign.dto.*;
import com.hualude.roadsign.model.PortRedRuleEntity;
import com.hualude.roadsign.repository.AlarmRepository;
import com.hualude.roadsign.repository.PortBlueRuleRepository;
import com.hualude.roadsign.repository.PortRedRuleRepository;
import com.hualude.roadsign.repository.RuleGroupRepository;
import com.hualude.roadsign.service.RuleGroupService;
import com.hualude.roadsign.utils.UdpSender;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.List;

@Service
public class RuleGroupServiceImpl implements RuleGroupService {

    @Resource
    private RuleGroupRepository ruleGroupRepository;

    @Resource
    private PortBlueRuleRepository portBlueRuleRepository;

    @Resource
    private PortRedRuleRepository portRedRuleRepository;

    @Resource
    private AlarmRepository alarmRepository;

    @Override
    public DTResponseDto getRuleGroup4DT(DTRequestDto dtRequestDto, String orgIds, String content, String time) throws Exception {
        List<RuleGroupDto> dtos=ruleGroupRepository.getRuleGroup4DT(dtRequestDto,orgIds,content,time);
        Integer count=ruleGroupRepository.getRuleGroupCount4DT(dtRequestDto,orgIds,content,time);
        return new DTResponseDto(dtRequestDto.getDraw(),count,count,dtos);
    }

    @Override
    @Transactional
    public Integer modifyRuleGroup(RuleGroupDto ruleGroupDto) throws Exception {
        return ruleGroupRepository.save(ruleGroupDto.toBean()).getRuleGroupId();
    }

    @Override
    @Transactional
    public Integer deleteRuleGroupById(String ruleGroupId) throws Exception {
        String[] ruleGroupIdArray = ruleGroupId.split(",");
        for (String ruleGroupIds :ruleGroupIdArray) {
            ruleGroupRepository.deleteRuleGroupById(Integer.parseInt(ruleGroupIds));
            portBlueRuleRepository.deletePortBlueRuleByRuleModelId(Integer.parseInt(ruleGroupIds));
            portRedRuleRepository.deletePortRedRuleByRuleModelId(Integer.parseInt(ruleGroupIds));
        }
        return ruleGroupIdArray.length;
    }

    @Override
    public DTResponseDto getRedRuleByModelId(DTRequestDto dtRequestDto, Integer modelId) throws Exception {
        List<PortRedRuleDto> dtos=portRedRuleRepository.getRedRuleByModelId(dtRequestDto,modelId);
        Integer count=portRedRuleRepository.getRedRuleCountByModelId(dtRequestDto,modelId);
        return new DTResponseDto(dtRequestDto.getDraw(),count,count,dtos);
    }
    @Override
    public DTResponseDto getBlueRuleByModelId(DTRequestDto dtRequestDto, Integer modelId) throws Exception {
        List<PortRedRuleDto> dtos=portBlueRuleRepository.getBlueRuleByModelId(dtRequestDto,modelId);
        Integer count=portBlueRuleRepository.getBlueRuleCountByModelId(dtRequestDto,modelId);
        return new DTResponseDto(dtRequestDto.getDraw(),count,count,dtos);
    }

    @Override
    @Transactional
    public Integer addRedRule(PortRedRuleDto dto) throws Exception {
        return portRedRuleRepository.save(dto.toBean()).getRedRuleId();
    }

    @Override
    public Integer addBlueRule(PortBlueRuleDto dto) throws Exception {
        return portBlueRuleRepository.save(dto.toBean()).getBlueRuleId();
    }

    @Override
    public Integer updateModelRule(Integer ruleModelId) throws Exception {
        List<PortRedRuleEntity> red100Rules=portRedRuleRepository.get100RedRules(ruleModelId);
        List<PortRedRuleEntity> red200Rules=portRedRuleRepository.get200RedRules(ruleModelId);
        Integer red100Count=0,red200Count=0;
        StringBuilder red100Content=new StringBuilder();
        StringBuilder red200Content=new StringBuilder();
        for(PortRedRuleEntity item:red100Rules){
            String content;
            content = item.getRedRuleContent();
            if(content!=null&&content.length()>0){
                red100Count++;
                red100Content.append(",").append(Integer.valueOf(content,2).toString());//2进制转10
            }
        }

        for(PortRedRuleEntity item:red200Rules){
            String content=item.getRedRuleContent();
            if(content!=null&&content.length()>0){
                red200Count++;
                red200Content.append(",").append(Integer.valueOf(content,2).toString());//2进制转10
            }
        }
        StringBuilder cmd=new StringBuilder("nbsvr setrule ");
        cmd.append(System.currentTimeMillis()/1000);
        cmd.append(",").append(red100Count).append(red100Content).append(",").append(red200Count).append(red200Content);
        StringBuilder sendCmd=new StringBuilder().append(ruleModelId).append(",SFG,").append(cmd);

        Integer isSend=1;
        try{
            UdpSender sender=UdpSender.getInstance();
            sender.sendMsg(sendCmd.toString());
            isSend=1;
        }catch (Exception e){
            isSend=0;
            e.printStackTrace();
        }
        return isSend;
    }



    @Override
    @Transactional
    public Integer updateRedRule(PortRedRuleDto redRuleDto) {
        return portRedRuleRepository.updateRule(redRuleDto.getRedRuleId(),
                redRuleDto.getRedRuleLevel(),
                redRuleDto.getRedRuleContent(),
                redRuleDto.getRedRuleStatus(),
                redRuleDto.getRedRuleTag(),
                redRuleDto.getRedRuleFlag());
    }

    @Override
    @Transactional
    public Integer updateBlueRule(PortBlueRuleDto blueRuleDto) {
        return portBlueRuleRepository.updateRule(blueRuleDto.getBlueRuleId(),
                blueRuleDto.getBlueRuleLevel(),
                blueRuleDto.getBlueRuleName(),
                blueRuleDto.getBlueRuleContent(),
                blueRuleDto.getBlueRuleFlag());
    }

}
